package main.users;

import main.SQL.MsSQLConnect;

import javax.swing.table.AbstractTableModel;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

public class UsersTableModel extends AbstractTableModel {
    public HashMap <Integer, String> userRoles = new HashMap<>();
    private String[] columnNames = {"id", "Логин", "Имя пользователя", "Роль пользователя"};
    private ArrayList<FactageUser> data = new ArrayList<>();
    private Class[] types = new Class[]{Integer.class, String.class, String.class, String.class};

    public UsersTableModel(ArrayList<FactageUser> data) {
        this.data = data;
    }

    public UsersTableModel() {
        data.add(new FactageUser(null, null, "Загрузка таблицы пользователей", null, null));
        getDataFromSQL();
    }

    @Override
    public int getColumnCount() { return 4; }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public String getColumnName(int col) { return columnNames[col]; }

    @Override
    public Object getValueAt(int row, int col) {
        FactageUser user = this.data.get(row);
        switch( col ) {
            case 0: return user.getId();
            case 1: return user.getLogin();
            case 2: return user.getName();
            case 3: return user.getRoleName();
            default: return "Unknown";
        }
    }

    public ArrayList<FactageUser> getData() { return data; }

    boolean[] canEdit = new boolean[]{false, false, true, true};

    @Override
    public Class getColumnClass(int columnIndex) {
        return types[columnIndex];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit[columnIndex];
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        if (data.size() > row) {
            FactageUser user = data.get(row);
            switch( col ) {
                case 0: user.setId((Integer) value); break;
                case 1: user.setLogin((String) value); break;
                case 2: user.setName((String) value); break;
                case 3: user.setRoleName((String) value); break;
            }
        } else {
            FactageUser user = new FactageUser();
            switch( col ) {
                case 0: user.setId((Integer) value); break;
                case 1: user.setLogin((String) value); break;
                case 2: user.setName((String) value); break;
                case 3: user.setRoleName((String) value); break;
            }

            data.add(user);
        }
        fireTableCellUpdated(row, col);
    }

    public void getDataFromSQL() {
        try {
            this.data = new ArrayList<>();
            Connection connSQL = MsSQLConnect.ConnectDB("Factage");

            Statement stat = connSQL.createStatement();
            ResultSet rs = stat.executeQuery( "SELECT * FROM user_roles ORDER BY id" );

            userRoles = new HashMap<>();
            while (rs.next()) {
                userRoles.put(rs.getInt("id"), rs.getString("name"));
            }

            rs = stat.executeQuery( "SELECT * FROM users ORDER BY id" );
            while (rs.next()) {
                FactageUser user = new FactageUser(rs.getInt("id"), rs.getString("login"), rs.getString("name"), rs.getInt("role_id"), userRoles.get(rs.getInt("role_id")));
                this.data.add(user);
            }

            rs.close();
            stat.close();

            connSQL.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public HashMap <Integer, String> getUserRoles() {
        return userRoles;
    }


}